#include "main.h"
#include "app_oled.h"
#include "rtc.h"
extern const unsigned char LOGO[];
extern const unsigned char tubiao[][8];


OLED oled;

void OLED_HAL_Init()
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
  __HAL_RCC_GPIOB_CLK_ENABLE();
	__HAL_RCC_GPIOA_CLK_ENABLE();
	
	GPIO_InitStruct.Pin = OLED_VCC_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(OLED_VCC_PORT, &GPIO_InitStruct);

	OLED_PWR_ON;

	OLED_GPIO_Init();
}


//void oled_showSN(void)
//{
//	unsigned char text[21];
//	uint8_t len=0;
//	len = sprintf((char *)text,"SN[0]:%08X",air.cpuid[0]);
//	text[len+1]='\0';
//	OLED_ShowString(0,1,text,SIZE8);
//	len = sprintf((char *)text,"SN[1]:%08X",air.cpuid[1]);
//	text[len+1]='\0';
//	OLED_ShowString(0,2,text,SIZE8);
//	len = sprintf((char *)text,"SN[2]:%08X",air.cpuid[2]);
//	text[len+1]='\0';
//	OLED_ShowString(0,3,text,SIZE8);
//}
void oled_showText(uint8_t * text,uint8_t len)
{
	text[len]='\0';
	OLED_ShowString(0,4,text,SIZE16);
}

void oled_updatebuff()
{
	uint8_t y,x;
	
	for(y=0;y<8;y++)
	{
		OLED_Set_Pos(0,y);
		for(x=0;x<128;x++)
		{
			OLED_WR_Byte(oled.oled_buff[(y*128)+x],OLED_DATA);
		}	
	}
}

void oled_updatetime()
{
	char showtime[20]={0};
	uint8_t len=0;
	EA0;
	
	RTC_UPDATE();
	HAL_RTC_GetDate(&hrtc, &(datetime.date), RTC_FORMAT_BIN);
	HAL_RTC_GetTime(&hrtc, &(datetime.time), RTC_FORMAT_BIN);
	
	len = sprintf((char*)showtime,"20%02d-%02d-%02d",\
		datetime.date.Year,datetime.date.Month,datetime.date.Date);
	if(len>18)len=18;
	showtime[len+1] = '\0';
	OLED_ShowString(0,0,(uint8_t *)showtime,SIZE16);
	
	len = sprintf((char*)showtime,"%02d:%02d:%02d",\
		datetime.time.Hours,datetime.time.Minutes,datetime.time.Seconds);
	if(len>18)len=18;
	showtime[len+1] = '\0';
	OLED_ShowString(0,2,(uint8_t *)showtime,SIZE16);
	EA1;
}


void oled_show1(void)
{
	
}


void oled_show2(void)
{
	
}


void oled_main()
{ 
//	uint8_t cnt=0;
	//OLED_SH1106_Init();
	OLED_SSD1306_Init();
	delay_msx(3);
	EA0;
	OLED_DrawBMP(0,0,128,8,(unsigned char *)LOGO);
	EA1;
	delay_ms(3000);
	OLED_Clear();
	oled_updatetime();
	
	while(1)
	{
		if(oled.oled_mode==0)
		{
			delay_ms(1000);
			oled_updatetime();
		}else
		{
			delay_ms(20);
			oled_updatebuff();
		}	
	}
}


